<?php
class Json {
	public function isJson($string)
	{
		return ! empty ( $string ) && is_string ( $string ) && preg_match ( '/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/', $string );
	}
	/**
	 * Formats a JSON string for pretty printing
	 *
	 * @param string $json The JSON to make pretty
	 * @param bool $html Insert nonbreaking spaces and <br />s for tabs and linebreaks
	 * @return string The prettified output
	 * @author Jay Roberts
	 */
	public function formatJson($json, $html = false, $tabspaces = null)
    {
        $tabcount = 0;
        $result = '';
        $inquote = false;
        $ignorenext = false;
		if ($html)
		{
			$tab = str_repeat ( "&nbsp;", ($tabspaces == null ? 4 : $tabspaces) );
			$newline = "<br/>";
		} else
		{
			$tab = ($tabspaces == null ? "\t" : str_repeat ( " ", $tabspaces ));
			$newline = "\n";
		}

        for($i = 0; $i < strlen($json); $i++) {
            $char = $json[$i];

            if ($ignorenext) {
                $result .= $char;
                $ignorenext = false;
            } else {
                switch($char) {
                    case ':':
                        $result .= $char . (!$inquote ? " " : "");
                        break;
                    case '{':
                        if (!$inquote) {
                            $tabcount++;
                            $result .= $char . $newline . str_repeat($tab, $tabcount);
                        }
                        else {
                            $result .= $char;
                        }
                        break;
                    case '}':
                        if (!$inquote) {
                            $tabcount--;
                            $result = trim($result) . $newline . str_repeat($tab, $tabcount) . $char;
                        }
                        else {
                            $result .= $char;
                        }
                        break;
                    case ',':
                        if (!$inquote) {
                            $result .= $char . $newline . str_repeat($tab, $tabcount);
                        }
                        else {
                            $result .= $char;
                        }
                        break;
                    case '"':
                        $inquote = !$inquote;
                        $result .= $char;
                        break;
                    case '\\':
                        if ($inquote) $ignorenext = true;
                        $result .= $char;
                        break;
                    default:
                        $result .= $char;
                }
            }
        }

        return $result;
    }
	/**
	 * Indents a flat JSON string to make it more human-readable.
	 *
	 * @param string $json
	 *        	The original JSON string to process.
	 *        	
	 * @return string Indented version of the original JSON string.
	 */
	public function indent($json) {
		$result = '';
		$pos = 0;
		$strLen = strlen ( $json );
		$indentStr = '  ';
		$newLine = "\n";
		$prevChar = '';
		$outOfQuotes = true;
		for($i = 0; $i <= $strLen; $i ++) {
			// Grab the next character in the string.
			$char = substr ( $json, $i, 1 );
			// Are we inside a quoted string?
			if ($char == '"' && $prevChar != '\\') {
				$outOfQuotes = ! $outOfQuotes;
				// If this character is the end of an element,
				// output a new line and indent the next line.
			} else if (($char == '}' || $char == ']') && $outOfQuotes) {
				$result .= $newLine;
				$pos --;
				for($j = 0; $j < $pos; $j ++) {
					$result .= $indentStr;
				}
			}
			// Add the character to the result string.
			$result .= $char;
			// If the last character was the beginning of an element,
			// output a new line and indent the next line.
			if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
				$result .= $newLine;
				if ($char == '{' || $char == '[') {
					$pos ++;
				}
				for($j = 0; $j < $pos; $j ++) {
					$result .= $indentStr;
				}
			}
			$prevChar = $char;
		}
		return $result;
	}
	
	/**
	 * 功能：进行json解码
	 *
	 * @param json $json        	
	 * @return string
	 */
	public static function decode($json) {
		$comment = false;
		$out = '$x=';
		for($i = 0; $i < strlen ( $json ); $i ++) {
			if (! $comment) {
				if ($json [$i] == '{')
					$out .= ' array(';
				else if ($json [$i] == '}')
					$out .= ')';
				else if ($json [$i] == ':')
					$out .= '=>';
				else
					$out .= $json [$i];
			} else
				$out .= $json [$i];
			if ($json [$i] == '"')
				$comment = ! $comment;
		}
		eval ( $out . ';' );
		// var_dump($out);
		return $x;
	}
	
	/**
	 * 功能：进行json编码
	 *
	 * @param array $array        	
	 * @return unknown
	 */
	public static function encode($arr) {
		$parts = array ();
		foreach ( $arr as $key => $value ) {
			if (is_array ( $value )) {
				if ($is_list)
					$parts [] = self::encode ( $value );
				else
					$parts [] = '"' . $key . '":' . self::encode ( $value );
			} else {
				$str = '';
				if (! $is_list)
					$str = '"' . $key . '":';
				if (is_numeric ( $value )) {
					$str .= $value;
				} elseif ($value === false) {
					$str .= 'false';
				} elseif ($value === true) {
					$str .= 'true';
				} else {
					$str .= '"' . addslashes ( $value ) . '"';
				}
				$parts [] = $str;
			}
		}
		$json = implode ( ',', $parts );
		return '{' . $json . '}';
	}
	
	public static function arrIsEmpty($arr = null) {
		if (is_array ( $arr )) {
			foreach ( $arr as $k => $v ) {
				if ($v && ! is_array ( $v )) {
					return false;
				}
				$t = self::arrIsEmpty ( $v );
				if (! $t) {
					return false;
				}
			}
			return true;
		} elseif (! $arr) {
			return true;
		} else {
			return false;
		}
	}
}
?>